let currentIndex = 0;
const items = document.querySelectorAll('.carousel-item');
const carouselInner = document.querySelector('.carousel-inner');
let intervalId;

function showSlide(index) {
    items.forEach((item, i) => {
        item.style.opacity = i === index ? 1 : 0;
    });
    carouselInner.style.transform = `translateX(-${index * 100}%)`;
}

function nextSlide() {
    currentIndex = (currentIndex + 1) % items.length;
    showSlide(currentIndex);
    restartInterval();
}

function prevSlide() {
    currentIndex = (currentIndex - 1 + items.length) % items.length; // 加上 items.length 是为了防止 currentIndex 变为负数
    if (currentIndex === items.length - 1) {
        currentIndex = 0; // 如果当前索引是最后一张图片，则跳转到第一张图片
    }
    showSlide(currentIndex);
    restartInterval();
}

function restartInterval() {
    clearInterval(intervalId);
    intervalId = setInterval(nextSlide, 3000);
}

// 初始化显示第一张图片
showSlide(currentIndex);

// 启动自动轮播
restartInterval();
